/*xj1PUwebAfMJ0Skz8sEQfPcDflh9NKsfUqkR1118VRrW/7zGxzPznKzWlYvkkRR5UMdmtGS1WmKM
R1KxzRy+rg==*/
import { ajax } from "nc-lightapp-front";


/**
 * 金额、税、汇率相关字段
 */
let moneyAndRateFields = ["money_bal", "money_de", "money_cr", "notax_de", "notax_cr", "occupationmny", "settlemoney", "nosubtax", "nosubtaxrate", "local_money_bal", "local_money_de", "local_money_cr",
	"local_notax_de", "local_notax_cr", "local_tax_de", "local_tax_cr", "caltaxmny", "groupbalance", "groupdebit", "groupcrebit", "groupnotax_de", "groupnotax_cre", "groupagentreceivelocal",
	"grouptax_de", "grouptax_cre", "globalbalance", "globaldebit", "globalcrebit", "globalnotax_de", "globalnotax_cre", "globalagentreceivelocal", "globaltax_de", "globaltax_cre",
	"price", "taxprice", "postprice", "postpricenotax", "local_price", "local_taxprice", "rate", 'taxcodeid', 'taxrate', 'taxtype', "grouprate", "globalrate", "quantity_bal"]

/**
 * 非元数据字段票据号checkno_display编辑后特殊处理
 * @param {*} props 
 * @param {*} tableId 
 * @param {*} key 
 * @param {*} value 
 * @param {*} index 
 */
function checknoDisplayAfterEvent(props, tableId, key, value, index) {
	if (key != 'checkno_display') {
		return
	}
	//将checkno_display字段的值赋给checkno
	if (value && (value.refpk||value.value)) {
		//参照
		let pk =value.refpk?value.refpk:value.value;
		let displayValue = value.refname?value.refname:value.display;
		props.cardTable.setValByKeyAndIndex(tableId, index, 'checkno', { value: pk, display: displayValue })
		props.cardTable.setValByKeyAndIndex(tableId, index, 'checkno_display', { value: displayValue, display: displayValue })
	} else {
		if (value instanceof Object) {
			//清空
			props.cardTable.setValByKeyAndIndex(tableId, index, 'checkno', { value: null, display: null })
		} else {
			//字符串
			props.cardTable.setValByKeyAndIndex(tableId, index, 'checkno', { value: value, display: value })
		}
	}

}

/**
 * 根据票据类型动态修改票据号字段类型
 * @param {*} props 
 * @param {*} tableId 
 * @param {*} pk_org_v 
 * @param {*} checktype 
 */
function modifyChecknoItemtype(props, tableId, pk_org_v, checktype) {
	if (!checktype) {
		return
	}
	let billclass = props.cardTable.getValByKeyAndIndex(tableId, 0, 'billclass').value;
	let meta = props.meta.getMeta()
	var destEditAreaCode = meta['gridrelation']['bodys'].destEditAreaCode;
	meta[destEditAreaCode].items.map((item) => {
		if (item.attrcode == 'checkno_display') {
			let needref = isChecknoRef(checktype, pk_org_v);
			if (needref && item.itemtype != 'refer') {
				item.itemtype = 'refer';
				if (billclass == 'fk') {
					item.refcode = 'fbm/refer/fbm/Bill4CmpPayGridRef/index.js';
				} else if (billclass == 'sk') {
					item.refcode = 'fbm/refer/fbm/Bill4CmpReceiveGridRef/index.js';
				}
			} else if (needref && item.itemtype == 'refer') {
				if (billclass == 'fk') {
					item.refcode = 'fbm/refer/fbm/Bill4CmpPayGridRef/index.js';
				} else if (billclass == 'sk') {
					item.refcode = 'fbm/refer/fbm/Bill4CmpReceiveGridRef/index.js';
				}
			} else {
				item.itemtype = 'input';
				item.refcode = null;
			}
		}
		return item;
	})
	props.meta.setMeta(meta);
}

/**
 * 票据类型checktype编辑后特殊处理
 * @param {*} props 
 * @param {*} formId 
 * @param {*} tableId 
 * @param {*} key 
 * @param {*} value 
 * @param {*} index 
 */
function checktypeAfterEvent(props, tableId, key, value, index) {
	// if (key != 'checktype' || !value.refpk) {
	// 	return
	// }
	if (key != 'checktype') {
		return
	}
	//编辑票据类型清空票据号
	props.cardTable.setValByKeyAndIndex(tableId, index, 'checkno_display', { value: null, display: null })
	props.cardTable.setEditableByIndex(tableId, index, 'checkno_display', true);
	props.cardTable.setValByKeyAndIndex(tableId, index, 'direct_checkno', { value: false, display: false });
	//根据票据类型动态修改票据号字段类型
	let pk_org_v = props.cardTable.getValByKeyAndIndex(tableId, index, 'pk_org').value
	modifyChecknoItemtype(props, tableId, pk_org_v, value.refpk)
	//根据票据类型控制直联电票是否可编辑
	let checktype = props.cardTable.getValByKeyAndIndex(tableId, index, 'checktype')
	//根据接口返回值判断直联电票是否可编辑
	if (checktype&&checktype.value!="") {
		ajax({
			url: "/nccloud/arap/arappub/checketcforbusi.do",
			data: {
				pk_etcbilltype: checktype.value,
			},
			async: false,
			success: (res) => {
				if (res.data&&res.data==true) {
					props.cardTable.setEditableByIndex(tableId, index, 'direct_checkno', true);
				} else {
					props.cardTable.setEditableByIndex(tableId, index, 'direct_checkno', false);
				}
			}
		})
	} else {
		props.cardTable.setEditableByIndex(tableId, index, 'direct_checkno', false);
	}
}


/**
 * 根据票据类型和财务组织判断票据号是否参照类型
 * @param {*} checktype 
 * @param {*} pk_org 
 */
function isChecknoRef(checktype, pk_org_v) {
	let editflag = false;
	ajax({
		url: "/nccloud/arap/arappub/ischecknoref.do",
		data: {
			checktype: checktype,
			pk_org: pk_org_v
		},
		async: false,
		success: (res) => {
			editflag = res.data;
		}
	})
	return editflag;
}
/**
 * 根据往来对象和财务组织、单据大类判断票据类型是否可修改
 * @param {*} checktype 
 * @param {*} pk_org 
 */
function isCheckTypeEdit(objtype, pk_org, billclass) {
	let editflag = false;
	ajax({
		url: "/nccloud/arap/arappub/ischecknoref.do",
		data: {
			pk_org: pk_org,
			objtype: objtype,
			billclass: billclass
		},
		async: false,
		success: (res) => {
			editflag = res.data;
		}
	})
	return editflag;
}
function reWriteCheckno(props, tableId) {
	let checknoValues = props.cardTable.getColValue(tableId, 'checkno_display', false, false)
	for (var i = 0; i < checknoValues.length; i++) {
		let checkno_display = checknoValues[i]
		if (!checkno_display.display) {
			props.cardTable.setValByKeyAndIndex(tableId, i, 'checkno_display', { value: checkno_display.value, display: checkno_display.value })
		}
	}
}


export { checknoDisplayAfterEvent, modifyChecknoItemtype, checktypeAfterEvent, isChecknoRef, reWriteCheckno, moneyAndRateFields, isCheckTypeEdit };

/*xj1PUwebAfMJ0Skz8sEQfPcDflh9NKsfUqkR1118VRrW/7zGxzPznKzWlYvkkRR5UMdmtGS1WmKM
R1KxzRy+rg==*/